
# coding=utf-8

class VehicleDetectResult:

    def __init__(self, roadpart: int, roadpartx: float, roadparty: float, direction: int, camera:str):

        self._dict = {
            "roadpart": int(roadpart),
            "roadpartx": float(roadpartx),
            "roadparty": float(roadparty),
            "direction": int(direction),
            "camera": str(camera),
            "carinfo": list([])
        }


    def addCarInfo(self, num: int,
           carid: str, cartype: int, carcolor: int, roadlane: int,
           distance: int, curx: float = None, cury: float = None):

        self._dict["carinfo"].append(dict({
            "num": int(num),
            "carid": str(carid),
            "cartype": int(cartype),
            "carcolor": int(carcolor),
            "roadlane": int(roadlane),
            "distance": int(distance),
            "curx": float(curx),
            "cury": float(cury)
        }))

    @property
    def dict(self):
        return self._dict


class VehicleDetectCarCount:

    def __init__(self, roadpart: int, roadpartx: float, roadparty: float, direction: int, count: int):

        self._dict = {
            "roadpart": int(roadpart),
            "roadpartx": float(roadpartx),
            "roadparty": float(roadparty),
            "direction": int(direction),
            "count": int(count)
        }

    @property
    def dict(self):
        return self._dict


class VehicleDetectMsg:

    def __init__(self, code: int, timestamp: int, msg: str, result=None):
        self._dict = {
            "code": code,
            "timestamp": timestamp,
            "msg": msg,
            "result": {}
        }
        if result is not None:
            self._dict["result"] = result.dict


    def updateResult(self, result:VehicleDetectResult):
        self._dict["result"] = result.dict


    def addCarInfo(self, num: int,
            carid: str, cartype: int, carcolor: int, roadlane: int,
            distance: int, curx: float = None, cury: float = None):

        self._dict["result"]["carinfo"].append({
            "num": num,
            "carid": carid,
            "cartype": cartype,
            "carcolor": carcolor,
            "roadlane": roadlane,
            "distance": distance,
            "curx": curx,
            "cury": cury
        })


    @property
    def dict(self):
        return self._dict



def makeDemoMsg():

    code = 0
    timestamp = 1637112067
    msg = u"成功"

    roadpart = 10
    roadpartx = 36.3131600729
    roadparty = 36.3131600729
    direction = 1

    vdr = VehicleDetectResult(roadpart, roadpartx, roadparty, direction)
    vdr.addCarInfo(carid=u"沪a8888", cartype=1, carcolor=2, distance=100,
                   curx=36.3131600729, cury=36.3131600729, roadlane=3)
    vdr.addCarInfo(carid=u"沪a7777", cartype=1, carcolor=2, distance=200,
                   curx=36.3131600729, cury=36.3131600729, roadlane=3)
    vdr.addCarInfo(carid=u"沪a9999", cartype=1, carcolor=2, distance=300,
                   curx=36.3131600729, cury=36.3131600729, roadlane=3)


    vdm = VehicleDetectMsg(code, timestamp, msg, vdr)

    return vdm


if __name__ == '__main__':
    vdm = makeDemoMsg()
    print(str(vdm.dict))
